/*
    This file is part of XCOM Mod Manager.

    XCOM Mod Manager is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    XCOM Mod Manager is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with XCOM Mod Manager.  If not, see <http://www.gnu.org/licenses/>.
*/
/*
 * @author Anthony Surma
 */

import java.util.Arrays;
import java.util.LinkedList;
import org.xml.sax.Attributes;
import org.xml.sax.helpers.DefaultHandler;

public class DocHandler extends DefaultHandler 
{   
    private LinkedList<String> results = new LinkedList<>();
    private String searchString;  
    private boolean update = false;
                       
    DocHandler(String searchString)
    {
        this.searchString = searchString;
    }
    
    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes)
    {
        if (qName.equals(searchString)) update = true;        
    }
    
    @Override
    public void characters(char ch[], int start, int length) 
    {
        if (update) 
        {
            results.add(new String(Arrays.copyOf(ch, ch.length), start, length));    
            update = false;
        }        
    }
    
    public void reset()
    {
        results = new LinkedList<>();
    }
    
    public String[] getResults()
    {
        String[] resultArray = new String[results.size()];
        int cur = -1;
        
        for (String s : results) resultArray[++cur] = s;
     
        return resultArray;
    }
}
